package com.xuhao.didi.oksocket;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class TextUtile implements Animator.StateChangedListener {
    private Text edText;
    private int size;
    private TextField textField;
    private Text text;
    private CommonDialog commonDialog;

    /**
     * textfide动画
     *
     * @param edAnimator 文本
     * @param isHint 显隐
     * @param size 字体大小
     */
    public void animatorField(Text edAnimator, boolean isHint, int size) {
        edText = edAnimator;
        this.size = size;
        AnimatorProperty animator = edAnimator.createAnimatorProperty();
        animator.setStateChangedListener(this);
        if (isHint) {
            animator.moveByY(-60).setCurveType(Animator.CurveType.LINEAR).setDuration(50);
        } else {
            animator.moveByY(60).setCurveType(Animator.CurveType.LINEAR).setDuration(50);
        }
        animator.start();
    }

    @Override
    public void onStart(Animator animator) {
        edText.setTextSize(size, Text.TextSizeType.FP);
    }

    @Override
    public void onStop(Animator animator) {
        animator.stop();
    }

    @Override
    public void onCancel(Animator animator) {
    }

    @Override
    public void onEnd(Animator animator) {
    }

    @Override
    public void onPause(Animator animator) {
    }

    @Override
    public void onResume(Animator animator) {
    }

    /**
     * 土司
     *
     * @param text 文本
     * @param context 上下文
     */
    public void toastText(String text, Context context) {
        DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_layout_toast, null, false);
        Text componentById = (Text) layout.findComponentById(ResourceTable.Id_tost);
        componentById.setText(text);
        new ToastDialog(context).setComponent(layout).setOffset(0,100).setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT).show();
    }

    /**
     * 弹框
     *
     * @param context 上下文
     * @param title 文本内容
     * @param name 名字
     * @param cancel 关闭
     * @param loqin 确定
     * @return layout布局
     */
    public Component commonDialog(Context context, String title, String name, String cancel, String loqin) {
        DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_dialog, null, false);
        CommonDialog commonDialog = new CommonDialog(context);
        commonDialog.setContentCustomComponent(layout);
        commonDialog.setSize(vp2px(context,300), DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        commonDialog.setSwipeToDismiss(true);
        commonDialog.setAutoClosable(true);
        commonDialog.siteRemovable(true);
        TextField mPass = (TextField) layout.findComponentById(ResourceTable.Id_pass);
        setText(mPass);
        Text mLogin = (Text) layout.findComponentById(ResourceTable.Id_login);
        mLogin.setText(loqin);
        Text mCancel = (Text) layout.findComponentById(ResourceTable.Id_cancel);
        mCancel.setText(cancel);
        mCancel.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                commonDialog.destroy();
            }
        });
        setDialogBtn(mLogin);
        Text mTitleText = (Text) layout.findComponentById(ResourceTable.Id_title_text);
        mTitleText.setText(title);
        Text mTitle = (Text) layout.findComponentById(ResourceTable.Id_title);
        mTitle.setText(name);
        commonDialog.show();
        setCommonDialog(commonDialog);
        return layout;
    }

    private int vp2px(Context context, float vp) {
        DisplayAttributes attributes = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes();
        return (int) (attributes.densityPixels * vp);
    }

    private void setText(TextField textField) {
        this.textField = textField;
    }

    public TextField getText() {
        return textField;
    }

    private void setDialogBtn(Text cancle) {
        this.text = cancle;
    }

    public Text getCancel() {
        return text;
    }

    private void setCommonDialog(CommonDialog commonDialog) {
        this.commonDialog = commonDialog;
    }

    public CommonDialog getCommonDialog() {
        return commonDialog;
    }

    /**
     * text置灰
     *
     * @param context 上下文
     * @param mIp textfield
     * @param mPost textfield
     */
    public void chickColor(Context context,TextField mIp, TextField mPost) {
        mIp.setTextColor(new Color(Color.getIntColor("#7A7A7A")));
        mPost.setTextColor(new Color(Color.getIntColor("#7A7A7A")));
        ShapeElement errorElement = new ShapeElement(context, ResourceTable.Graphic_background_untextfild_color);
        mIp.setBasement(errorElement);
        mPost.setBasement(errorElement);
    }

    /**
     * text恢复
     *
     * @param context 上下文
     * @param mIp textfield
     * @param mPost textfield
     */
    public void chickTextColor(Context context,TextField mIp, TextField mPost) {
        mIp.setTextColor(new Color(Color.getIntColor("#050505")));
        mPost.setTextColor(new Color(Color.getIntColor("#050505")));
        ShapeElement errorElement = new ShapeElement(context, ResourceTable.Graphic_background_ctextfild_color);
        mIp.setBasement(errorElement);
        mPost.setBasement(errorElement);
    }

    public void textInfo(TextField mIp, Text edAnimator, int size) {
        if (mIp.isFocused()) {
            AnimatorProperty animator = edAnimator.createAnimatorProperty();
            animator.moveByY(60).setCurveType(Animator.CurveType.LINEAR).setDuration(50);
            animator.setStateChangedListener(new Animator.StateChangedListener() {
                @Override
                public void onStart(Animator animator) {
                    edAnimator.setTextSize(size, Text.TextSizeType.FP);
                }

                @Override
                public void onStop(Animator animator) {
                }

                @Override
                public void onCancel(Animator animator) {
                }

                @Override
                public void onEnd(Animator animator) {
                }

                @Override
                public void onPause(Animator animator) {
                }

                @Override
                public void onResume(Animator animator) {
                }
            });
            animator.start();
            edAnimator.setTextColor(new Color(Color.getIntColor("#7A7A7A")));
            mIp.clearFocus();
        }
    }
}

